package com.qeegoo.b2bautozityp.moudle.inquiry.adapter;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.view.OrderDetailsActivity;
import com.qeegoo.b2bautozityp.moudle.inquiry.model.InquiryListBean;
import com.qeegoo.b2bautozityp.moudle.inquiry.model.InquiryListBean.ListBean;
import com.qeegoo.b2bautozityp.moudle.inquiry.model.InquiryListBean.ListBean.GoodsInfoMoreListBean;

import java.util.ArrayList;
import java.util.List;

import base.lib.util.NavigateUtils;

/**
 * Created by lining on 2017/9/29.
 */

public class InquiryFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<InquiryListBean.ListBean> datas;

    public InquiryFragmentAdapter() {
        datas = new ArrayList<>();
    }

    public void addAllDatas(List<InquiryListBean.ListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addDatas(List<InquiryListBean.ListBean> data) {
        if (datas != null) {
            datas.addAll(data);
        }
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolderChild(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_child_inpuiry, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListBean listBean = datas.get(position);
        ((ViewHolderChild) holder).mCarinfo.setText("x"+listBean.carModelName);
        ((ViewHolderChild) holder).mGoodsAddress.setText(listBean.areaName);
        ((ViewHolderChild) holder).mGoodsInvoice.setText((listBean.needInvoice ==1)?"要发票":"不要票");


        Glide.with( ((ViewHolderChild) holder).mCarLogo.getContext())
                .load(listBean.carLogoUrl)
                .centerCrop()
                .into(((ViewHolderChild) holder).mCarLogo);
        List<GoodsInfoMoreListBean> goodsInfoList = listBean.goodsInfoMoreList;
        switch (goodsInfoList.size()){
            case 0:{
                ((ViewHolderChild) holder).mGoods1.setVisibility(View.GONE);
                ((ViewHolderChild) holder).mGoods2.setVisibility(View.GONE);
                break;
            }
            case 1:{
                ((ViewHolderChild) holder).mGoods1.setText(goodsInfoList.get(0).category+"      X"+goodsInfoList.get(0).amount);
                ((ViewHolderChild) holder).mGoods1.setVisibility(View.VISIBLE);
                ((ViewHolderChild) holder).mGoods2.setVisibility(View.GONE);
                break;
            }
            case 2:{
                ((ViewHolderChild) holder).mGoods1.setText(goodsInfoList.get(0).category+"      X"+goodsInfoList.get(0).amount);
                ((ViewHolderChild) holder).mGoods2.setText(goodsInfoList.get(1).category+"      X"+goodsInfoList.get(1).amount);
                ((ViewHolderChild) holder).mGoods1.setVisibility(View.VISIBLE);
                ((ViewHolderChild) holder).mGoods2.setVisibility(View.VISIBLE);
                break;
            }
            default:
                ((ViewHolderChild) holder).mGoods1.setText(goodsInfoList.get(0).category+"      X"+goodsInfoList.get(0).amount);
                ((ViewHolderChild) holder).mGoods2.setText(goodsInfoList.get(1).category+"      X"+goodsInfoList.get(1).amount);
                ((ViewHolderChild) holder).mGoods1.setVisibility(View.VISIBLE);
                ((ViewHolderChild) holder).mGoods2.setVisibility(View.VISIBLE);
                break;
        }
        ((ViewHolderChild) holder).mTime.setText("询价时间: "+listBean.createTime);
        ((ViewHolderChild) holder).mAskPrice.setVisibility(listBean.scopeStatus==0?View.VISIBLE:View.GONE);
        ((ViewHolderChild) holder).mLookPrice.setVisibility(listBean.scopeStatus==1?View.VISIBLE:View.GONE);
        ((ViewHolderChild) holder).mAskPrice.setOnClickListener(v -> {
            Bundle params = new Bundle();
            params.putString(OrderDetailsActivity.ID_GOODS,listBean.id);
            params.putInt(OrderDetailsActivity.FRAGMENT_CATEGORY_TYPE,OrderDetailsActivity.GOTO_PRICE_DETAIL);
            NavigateUtils.startActivity(OrderDetailsActivity.class,params);



        });
        ((ViewHolderChild) holder).mLookPrice.setOnClickListener(v -> {
            Bundle params = new Bundle();
            params.putString(OrderDetailsActivity.ID_GOODS,listBean.id);
            params.putInt(OrderDetailsActivity.FRAGMENT_CATEGORY_TYPE,OrderDetailsActivity.LOOKFOR_PRICE_DETAIL);
            NavigateUtils.startActivity(OrderDetailsActivity.class,params);
        });
        ((ViewHolderChild) holder).mWrapLayout.setOnClickListener((v) -> {
            mOnItemClickListener.onItemClick(((ViewHolderChild) holder).mWrapLayout, position);
        });
    }

    @Override
    public int getItemCount() {
        int count = (datas == null ? 0 : datas.size());
        return count;
    }
    public static class ViewHolderChild extends RecyclerView.ViewHolder {

        private TextView mCarinfo;
        private TextView mGoodsAddress;
        private TextView mGoodsInvoice;
        private TextView mGoods1;
        private TextView mGoods2;
        private TextView mTime;
        private ImageView mCarLogo;
        private View mAskPrice;
        private View mLookPrice;
        private View mWrapLayout;

        public ViewHolderChild(View view) {
            super(view);
            mCarinfo = (TextView) view.findViewById(R.id.tv_car_info);
            mGoodsAddress = (TextView) view.findViewById(R.id.tv_goods_address);
            mGoodsInvoice = (TextView) view.findViewById(R.id.tv_goods_invoice);
            mCarLogo = (ImageView) view.findViewById(R.id.iv_car_logo);
            mGoods1 = (TextView) view.findViewById(R.id.tv_goods1);
            mGoods2 = (TextView) view.findViewById(R.id.tv_goods2);
            mTime = (TextView) view.findViewById(R.id.tv_ask_price_time);
            mAskPrice = view.findViewById(R.id.tv_ask_price);
            mLookPrice = view.findViewById(R.id.tv_look_price);
            mWrapLayout = view.findViewById(R.id.wrap_layout);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
